/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mifengs.mall.ui.hxcec;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.hyphenate.chat.ChatClient;
import com.hyphenate.chat.Conversation;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.MallApplication;
import com.mifengs.mall.R;
import com.mifengs.mall.ui.login.LoginActivity;


public class CecLoginActivity extends DemoBaseActivity {

	private static final String TAG = "CecLoginActivity";

	private boolean progressShow;
	private ProgressDialog progressDialog;
	private int selectedIndex = Constant.INTENT_CODE_IMG_SELECTED_DEFAULT;
	private int messageToIndex = Constant.MESSAGE_TO_DEFAULT;
	private String orderName="";
	private String orderPrice="";
	private String orderImg="";
	private String orderUrl="";
	private String orderId="";
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		Intent intent = getIntent();
		selectedIndex = intent.getIntExtra(Constant.INTENT_CODE_IMG_SELECTED_KEY,
				Constant.INTENT_CODE_IMG_SELECTED_DEFAULT);
//		messageToIndex = intent.getIntExtra(Constant.MESSAGE_TO_INTENT_EXTRA, Constant.MESSAGE_TO_DEFAULT);
		messageToIndex= 0;
		orderName = intent.getStringExtra("orderName");
		orderPrice = intent.getStringExtra("orderPrice");
		orderImg = intent.getStringExtra("orderImg");
		orderUrl = intent.getStringExtra("orderUrl");
		orderId= intent.getStringExtra("orderId");
		//ChatClient.getInstance().isLoggedInBefore() 可以检测是否已经登录过环信，如果登录过则环信SDK会自动登录，不需要再次调用登录操作
		if (ChatClient.getInstance().isLoggedInBefore()) {
			progressDialog = getProgressDialog();
			progressDialog.setMessage(getResources().getString(R.string.is_contact_customer));
			progressDialog.show();
			toChatActivity();
		} else {
			hxCecLogin();
		}

	}

	public void hxCecLogin() {
//		progressDialog = getProgressDialog();
//		if(progressDialog !=null){
//			progressDialog.setMessage(getResources().getString(R.string.is_contact_customer));
//			if (!progressDialog.isShowing()) {
//				progressDialog.show();
//			}
//		}

		String userName = PreferenceUtils.getHxUserName(MallApplication.getInstance());
		String userPwd = PreferenceUtils.getHxUserPwd(MallApplication.getInstance());
		if(!"".equals(userName) && !"".equals(userPwd)){
			ChatClient.getInstance().login(userName, userPwd, new com.hyphenate.helpdesk.callback.Callback() {
				@Override
				public void onSuccess() {
					toChatActivity();
				}

				@Override
				public void onError(int i, String s) {
					runOnUiThread(new Runnable() {
						public void run() {
//							progressDialog.dismiss();
							Toast.makeText(CecLoginActivity.this,
									getResources().getString(R.string.is_contact_customer_failure_seconed),
									Toast.LENGTH_SHORT).show();
							finish();
						}
					});
				}

				@Override
				public void onProgress(int i, String s) {

				}
			});
		}else{
			startActivity(new Intent(MallApplication.getInstance(), LoginActivity.class));
			finish();
		}

	}

	private ProgressDialog getProgressDialog() {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(CecLoginActivity.this);
			progressDialog.setCanceledOnTouchOutside(false);
			progressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					progressShow = false;
				}
			});
		}
		return progressDialog;
	}

	private void toChatActivity() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (!CecLoginActivity.this.isFinishing())
					if(progressDialog !=null)
					progressDialog.dismiss();

				//此处演示设置技能组,如果后台设置的技能组名称为[shouqian|shouhou],这样指定即分配到技能组中.
				//为null则不按照技能组分配,同理可以设置直接指定客服scheduleAgent
				String queueName = null;
				switch (messageToIndex){
					case Constant.MESSAGE_TO_DEFAULT:
						queueName = "蜜蜂商城蜂巢客服";
						break;
					case Constant.MESSAGE_TO_PRE_SALES:
						queueName = "小蜜蜂1";
						break;
					case Constant.MESSAGE_TO_AFTER_SALES:
						queueName = "小蜜蜂2";
						break;
					default:
						break;
				}
			 //设置点击通知栏跳转事件
				Conversation conversation = ChatClient.getInstance().chatManager().getConversation(Preferences.getInstance().getCustomerAccount());
				String titleName = null;
				if (conversation.officialAccount() != null){
					titleName = conversation.officialAccount().getName();
				}
				// 进入主页面
				Intent intent = new IntentBuilder(CecLoginActivity.this)
						.setTargetClass(ChatActivity.class)
//						.setVisitorInfo(DemoMessageHelper.createVisitorInfo())
						.setServiceIMNumber(Preferences.getInstance().getCustomerAccount())
//						.setScheduleQueue(DemoMessageHelper.createQueueIdentity(queueName))
						.setTitleName(Constant.HX_CUSTOMER_NAME)
//						.setScheduleAgent(DemoMessageHelper.createAgentIdentity("niecaixia@mifengs.com"))
						.setShowUserNick(true)
						.build();
				intent.putExtra("orderName",orderName);
				intent.putExtra("orderPrice",orderPrice);
				intent.putExtra("orderImg",orderImg);
				intent.putExtra("orderUrl",orderUrl);
				intent.putExtra("orderId",orderId);
				intent.putExtra("type",1);//0表示从会话列表进入  1表示从点击客服进入
				startActivity(intent);
				finish();

			}
		});
	}

}
